﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aspire
{
    /// <summary>
    /// 页面上的一个功能菜单
    /// </summary>
    public class Function
    {
        /// <summary>
        /// Function唯一标识
        /// </summary>
        public long ID { get; set; }
        /// <summary>
        /// 内部名称，必须为英文
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; set; }

        public long ParentFunctionID { get; set; }
        public long PluginPageID { get; set; }

        /// <summary>
        /// 子功能列表
        /// </summary>
        public List<Function> Functions { get; set; }

        /// <summary>
        /// 父功能
        /// </summary>
        public Function ParentFunction { get; set; }

        /// <summary>
        /// 对应的插件信息
        /// </summary>
        public PluginPackage Plugin { get { return Page.Package; } }
        /// <summary>
        /// 所属的业务组件
        /// </summary>
        public PluginPage Page { get; set; }

        /// <summary>
        /// 小图标
        /// </summary>
        public string SmallIconUrl { get; set; }
        
        /// <summary>
        /// 大图标
        /// </summary>
        public string BigIconUrl { get; set; }

        /// <summary>
        /// URL参数
        /// </summary>
        public string Arguments { get; set; }

        public string NavigateUri
        {
            get
            {
                Stack<Function> funcStack = new Stack<Function>();

                Function f = this;
                while (f != null)
                {
                    funcStack.Push(f);
                    f = f.ParentFunction;
                }

                StringBuilder sb = new StringBuilder();

                while (funcStack.Count > 0)
                {
                    sb.AppendFormat("/{0}", funcStack.Pop().Name);
                }

                if (!String.IsNullOrEmpty(this.Arguments))
                {
                    sb.Append("?");
                    sb.Append(this.Arguments);
                }

                return sb.ToString();
            }
        }

        /// <summary>
        /// 功能列表
        /// </summary>
        public Function()
        {
            Functions = new List<Function>();
        }

    }
}
